by K. David White
In This Chapter
File-based computing has been around for ages, or so it seems. Long before databases became the predominant data persistence mechanism, data was stored in files that resided on system disk. Although the database is currently persisted to a file system, the user is not concerned about how this file system is represented. There are many forms of data, and by the nature of the beast, it is apparent that binary representation of data in a file system presents a whole new set of problems.
Most applications use the file system in some fashion, and it is important for the MFC developer to understand what the file system represents as well as how to use it. In this chapter, you will discover that MFC provides a consistent interface, CFile, for handling data persistence to the file system. Youll also take a cursory look at serialization and what takes place under the covers there.
These classes are straightforward and online documentation is fairly adequate, but I want to take an in-depth look at how it fits together. When you understand how the file system is put together, you will better understand how to use it.
The MFC library provides the CFile class to handle normal I/O processing to the file system. This class provides basic nonbuffered file access that essentially wraps the Windows file API calls. There are derived classes of CFile that are used specifically for doing the file-related work. These are CStdioFile and CMemFile. The CStdioFile handles general I/O processing for ASCII type data buffers. The CMemFile handles processing for memory data.
Note:Serialization is another method to write information from the application to a file. Although this is primarily a function of the document/view architecture, I will take a look at it in this chapter. The MFC CDocument object framework handles serialization for you. It uses the CFile object. To take advantage of this, implement code in the Serialize member function of your derived CDocument object to process input and output.
The CFile class provides an interface for general-purpose binary file operations. The CStdioFile and CMemFile classes derived from CFile and the CSharedFile class derived from CMemFile supply more specialized file services.
A CStdioFile class represents stream-based file processing as opened by fopen. Stream files can be either text files or binary files, but the CStdioFile class handles the input and output in stream mode. The last section of this chapter provides some insight into file stream operations.
Note:The Duplicate, LockRange, and UnlockRange methods of CFile are not supported in CStdioFile.
Text mode, as with ASCII files, handles the carriage return-linefeed pair as an end-of-line. The newline character (0x0A), when applied to a string to be sent to the text-mode CStdioFile object, is actually written out as the byte pair (0x0A, 0x0D)(CR/LF). When that pair is read in, it translates it back to the newline character.
Lets take a look inside CFile!
This section takes a general look at processing file data using the CFile-derived classes.
To use CFile, you first must create a CFile with a filename and indicate how the file is to be opened (that is, its access mode, including privilege settings). With the CFile class, you have the option of defining the privileges either in the constructor, or when the file is opened. Table 21.1 is a list of possible modes. Modes define the access privileges for handling the file operations.
| Mode | Description/Notes |
|---|---|
| modeRead | Opens the file for reading only. |
| modeWrite | Opens the file for writing. |
| modeReadWrite | Opens the file for reading and writing. |
| modeCreate | Will create the file. If the file already exists, the size will be set to zero. |
| modeNoTruncate | Creates the file without truncating it. |
| modeNoInherit | Prevents the file from being inherited by a child process. |
| shareDenyRead | Denies Read access to the file from other applications/processes. |
| shareDenyWrite | Denies Write access to the file from other applications/processes. |
| shareDenyNone | Does not apply any sharing restrictions to the file. |
| shareExclusive | Denies Read and Write access to the file from other applications/processes. |
| shareCompat | Allows other processes to open the file any number of times. |
| typeBinary | Sets the binary mode for the file. This mode is exclusive to the CStdioFile class. |
| typeText | Sets the text mode for the file. This mode is exclusive to the CStdioFile class. |